package com.xingshang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingshang.model.po.Admin;
import com.xingshang.model.po.User;
import com.xingshang.service.AdminService;
import com.xingshang.service.UserService;
import com.xingshang.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

/**
 * @author 87547
 * @description 针对表【wn_user】的数据库操作Service实现
 * @createDate 2025-04-01 08:37:17
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    private final UserMapper userMapper;
    private final AdminService adminService;

    @Override
    public User login(String account, String password) {
        Admin admin = adminService.getUserByAccount(account);
        if (BeanUtil.isNotEmpty(admin)) {
            User u = new User();
            u.setAccount(account);
            u.setPassword(admin.getPassword());
            return u;
        }
        User user = userMapper.getUserByAccount(account);

        if (BeanUtil.isNotEmpty(user)) {
            String s = DigestUtils.md5DigestAsHex(password.getBytes());
            if (s.equals(user.getPassword())) {
                return user;
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public int insertUser(String account,
                          String password,
                          String email,
                          String telphone,
                          MultipartFile avatar) throws IOException {
        User userByAccount = userMapper.getUserByAccount(account);
        System.out.println("userByAccount = " + userByAccount);
        if (BeanUtil.isNotEmpty(userByAccount)) {
            return -1;
        }
        String path = this.saveAvatar(avatar);
        User user = new User();
        user.setAccount(account);
        user.setPassword(DigestUtil.md5Hex(password));
        user.setEmail(email);
        user.setTelphone(telphone);
        user.setAvatar(path);
        user.setPoints(0);
        user.setMoney(BigDecimal.valueOf(0.0));
        user.setRegTime(new java.util.Date());
        user.setStatus("1");
        return userMapper.insertUser(user);
    }

    @Override
    public String saveAvatar(MultipartFile avatarFile) throws IOException {
        String originalFilename = avatarFile.getOriginalFilename();
        String fileExt = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!fileExt.equals(".jpg") && !fileExt.equals(".png") && !fileExt.equals(".jpeg")) {
            throw new IllegalArgumentException("仅支持JPEG/JPG/PNG格式");
        }
// 生成唯一文件名
        String fileName = UUID.randomUUID() + fileExt;
// 获取项目根目录下的静态资源目录
        String uploadDir = "src/main/webapp/static/upload/avatar/";
        Path uploadPath = Paths.get(uploadDir);
// 确保目录存在
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }
        // 保存文件
        Path filePath = uploadPath.resolve(fileName);
        Files.copy(avatarFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
// 返回相对路径，前端可以直接访问
        return "/static/upload/avatar/" + fileName;
    }
}






